// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.chimi.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.chimi.controller.request.QueryBarPointFlowReq;
import com.liuxinlong.modules.chimi.controller.request.UpdateBarPointsFlowReq;
import com.liuxinlong.modules.chimi.controller.request.UpdateBatchBarPointsFlowReq;
import com.liuxinlong.modules.chimi.service.BarPointsFlowService;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.BarPointsFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 论坛积分流水控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2023-02-13
 */
@RestController
@RequestMapping("/points-flow")
public class BarPointsFlowController {
    @Autowired
    private BarPointsFlowService barPointsFlowService;

    /**
     * 分页查询积分流水信息
     *
     * @param req 请求信息
     * @return 积分流水信息列表
     */
    @PostMapping("page")
//	@LogAnnotation(value ="分页查询",business = LogBusinessConstants.BAR_POINTS_FLOW,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pagePointsFlowInfo(@RequestBody QueryBarPointFlowReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        queryParam.put("userId", req.getUserId());
        queryParam.put("sourceType", req.getSourceType());
        queryParam.put("startTime", req.getStartTime());
        queryParam.put("endTime", req.getEndTime());
        List<Map<String, Object>> dataList = barPointsFlowService.pagePointsFlow(queryParam);
        int total = barPointsFlowService.getPointsFlowCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        resp.setRetMessage("分页查询积分流水信息成功");
        return resp;
    }

    /**
     * 新增积分流水
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增积分流水", business = LogBusinessConstants.BAR_POINTS_FLOW, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp addPointsFlow(@RequestBody UpdateBarPointsFlowReq req) {
        BarPointsFlow barPointsFlow = new BarPointsFlow();
        barPointsFlow.setUserId(req.getUserId());
        barPointsFlow.setPoints(req.getPoints());
        barPointsFlow.setSourceType(req.getSourceType());
        barPointsFlow.setType(req.getType());
        barPointsFlow.setOperateTime(req.getOperateTime());
        barPointsFlowService.addPointsFlow(barPointsFlow);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增积分流水成功");
        return resp;
    }

    /**
     * 修改积分流水信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改积分流水信息", business = LogBusinessConstants.BAR_POINTS_FLOW, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updatePointsFlow(@RequestBody UpdateBarPointsFlowReq req) {
        BarPointsFlow barPointsFlow = new BarPointsFlow();
        barPointsFlow.setId(req.getId());
        barPointsFlow.setUserId(req.getUserId());
        barPointsFlow.setPoints(req.getPoints());
        barPointsFlow.setSourceType(req.getSourceType());
        barPointsFlow.setType(req.getType());
        barPointsFlow.setOperateTime(req.getOperateTime());
        barPointsFlowService.updatePointsFlow(barPointsFlow);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改积分流水信息成功");
        return resp;
    }

    /**
     * 删除积分流水信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除积分流水信息", business = LogBusinessConstants.BAR_POINTS_FLOW, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deletePointsFlow(@RequestBody BaseDeleteReq req) {
        barPointsFlowService.deletePointsFlow(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除积分流水信息成功");
        return resp;
    }

    /**
     * 用户分值校正
     */
    @GetMapping("check")
    @LogAnnotation(value = "用户分值校正", business = LogBusinessConstants.BAR_POINTS_FLOW, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp checkUserPoints() {
        int total = barPointsFlowService.checkUserPoints();
        BaseResp resp = new BaseResp();
        resp.setTotalNum(total);
        resp.setRetMessage("用户分值校正成功");
        return resp;
    }

    /**
     * 批量修改积分流水信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update-batch")
    @LogAnnotation(value = "批量修改积分流水信息", business = LogBusinessConstants.BAR_POINTS_FLOW, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateBatchPointsFlow(@RequestBody UpdateBatchBarPointsFlowReq req) {
        barPointsFlowService.updateBatchPointsFlow(req.getSourceType(), req.getPoints(), req.getStartTime(), req.getEndTime());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("批量修改积分流水信息成功");
        return resp;
    }

    /**
     * 查询积分排名
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("rank-page")
    @LogAnnotation(value = "查询积分排名", business = LogBusinessConstants.BAR_POINTS_FLOW, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp queryPointsRanking(@RequestBody QueryBarPointFlowReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        queryParam.put("status", req.getSourceType());
        queryParam.put("userId", req.getUserId());
        List<Map<String, Object>> dataList = barPointsFlowService.pagePointsRanking(queryParam);
        int totalNum = barPointsFlowService.getPointsRankingCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        resp.setRetMessage("查询积分排名信息成功");
        return resp;
    }

    /**
     *  修改积分排名配置
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("rank-update")
    @LogAnnotation(value = "修改积分排名配置", business = LogBusinessConstants.BAR_POINTS_FLOW, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updatePointsRanking(@RequestBody QueryBaseReq req) {
        barPointsFlowService.updatePointsRanking(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改积分排名配置信息成功");
        return resp;
    }
}
